package cn.itheima.manage.dao;

import cn.itheima.manage.domain.Student;

public class StudentDao {
    private Student[] students = new Student[5];

    public boolean addStudent(Student stu) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            students[index] = stu;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return students;
    }

    public void deleteStudentBySid(String sid) {
        int index = getIndex(sid);
        students[index] = null;
    }

    private int getIndex(String sid) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if(student!=null&&student.getId().equals(sid)){
                index = i;
            }
        }
        return index;
    }

    public void updateStudent(String sid, Student stu) {
        int index = getIndex(sid);
        students[index] = stu;
    }
}
